package com.wish.merchant.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.activity7.activiti.pojo.ActivitiVo;
import com.bw.activity7.activiti.pojo.UserTask;
import com.bw.activity7.activiti.service.ActivitiService;
import com.wish.common.core.utils.StringUtils;
import com.wish.common.core.utils.poi.ExcelUtil;
import com.wish.common.core.web.controller.BaseController;
import com.wish.common.core.web.domain.AjaxResult;
import com.wish.common.core.web.page.TableDataInfo;
import com.wish.common.log.annotation.Log;
import com.wish.common.log.enums.BusinessType;
import com.wish.common.security.annotation.RequiresPermissions;
import com.wish.common.security.utils.SecurityUtils;
import com.wish.merchant.entity.Merchant;
import com.wish.merchant.service.IMerchantService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 商家类型表 前端控制器
 * </p>
 *
 * @author lyk
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController extends BaseController {

    @Autowired
    IMerchantService merchantService;

    @Autowired
    ActivitiService activitiService;

    @GetMapping("/createDeployment")
    public void createDeployment(){
        activitiService.createDeployment("测试发布","wish1129","bpmn/wish1129.bpmn");
        System.err.println("发布");
    }

    @GetMapping("/list")
    @RequiresPermissions("merchant:merchant:list")
    public TableDataInfo list(Merchant merchant) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(merchant.getTenantName())) {
            queryWrapper.like("tenant_name", merchant.getTenantName());
        }
        if (StringUtils.hasText(merchant.getAddress())) {
            queryWrapper.like("address", merchant.getAddress());
        }
        if (StringUtils.hasText(merchant.getLegalPerson())) {
            queryWrapper.like("legal_person", merchant.getLegalPerson());
        }
        startPage();
        List<Merchant> list = merchantService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * 新增商户
     */
    //@RequiresPermissions("merchant:merchant:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Merchant Merchant)
    {
        Merchant.setCreateTime(LocalDateTime.now());
        boolean save = merchantService.save(Merchant);


        //提交审核 启动工作流实列 封装请求参数
        ActivitiVo activitiVo = new ActivitiVo();
        activitiVo.setUsername("zhangsan");
        activitiVo.setBusinessKey(Merchant.getId().toString());
        activitiVo.setDeploymentKey("wish1129");

        activitiVo.setVariables(new ArrayList<>());
        ProcessInstance processInstance = activitiService.askForBiz(activitiVo);
        System.err.println(processInstance);
        return toAjax(save);
    }

    @RequiresPermissions("merchant:merchant:query")
    @GetMapping("/queryUserTaskByUserName")
    public AjaxResult queryUserTaskByUserName(){
        ActivitiVo activitiVo = new ActivitiVo();
        String username = SecurityUtils.getUsername();
        activitiVo.setUsername(username);
        List<UserTask> userTasks = activitiService.queryUserTaskByUserName(activitiVo);
        return AjaxResult.success(userTasks);
    }

    @RequiresPermissions("merchant:merchant:complete")
    @PostMapping("/completeUserTaskById")
    public AjaxResult completeUserTaskById(@RequestBody ActivitiVo activitiVo){
        //根据token 获取当前用户
        String username = SecurityUtils.getUsername();
        activitiVo.setUsername(username);
        activitiService.completeUserTaskById(activitiVo);
        return AjaxResult.success();
    }


    /**
     * 导出商户列表
     */
    @RequiresPermissions("merchant:merchant:export")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Merchant merchant)
    {
        List<Merchant> list = merchantService.list();
        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
        util.exportExcel(response, list, "商户数据");
    }

    /**
     * 获取商户详细信息
     */
    @RequiresPermissions("merchant:merchant:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(merchantService.getById(id));
    }



    /**
     * 修改商户
     */
    @RequiresPermissions("merchant:merchant:edit")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Merchant merchant)
    {
        return toAjax(merchantService.updateById(merchant));
    }

    /**
     * 删除商户
     */
    @RequiresPermissions("merchant:merchant:remove")
    @Log(title = "商户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(merchantService.removeBatchByIds(Arrays.asList(ids)));
    }
}
